﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.Business;
using Actuals.Common;

using Actuals.CoreDomain.Data;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;

namespace Actuals
{
    public partial class DashBoard : System.Web.UI.Page
    {
        protected IDashBoard _dashBoard
        {
            get
            {
                if (ViewState["DashBoard"] == null)
                {
                    DateTime asOfDate;
                    string[] _date = txtDbAsOfDate.Text.Split('-');

                    asOfDate = new DateTime(int.Parse(_date[2]), int.Parse(_date[0]), int.Parse(_date[1]));

                    IBusinessDashBoard businessDashBoard = new BusinessDashBoard();
                    IDashBoard dashBoard = businessDashBoard.GetDashBoardAsOf(int.Parse(ddlProject.SelectedValue), int.Parse(ddlRelease.SelectedValue), asOfDate);
                    ViewState["DashBoard"] = dashBoard;
                    return dashBoard;

                }
                else
                {
                    return (IDashBoard)ViewState["DashBoard"];
                }

            }

        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EmployeeId"] == null)
            {
                Response.Write("Please login!");
                Response.End();
            }
            else
            {
                if (!Page.IsPostBack)
                {

                    IBusinessProject businessproject = new BusinessProject();
                    List<IProject> projects = businessproject.GetProjects();
                    ddlProject.DataSource = projects;
                    ddlProject.DataTextField = "ProjectDescription";
                    ddlProject.DataValueField = "ProjectId";
                    ddlProject.DataBind();
                    ddlProject.Items.Insert(0, "Select");

                    

                    
                }

            }


        }

        protected void ddlProject_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }

        protected void ddlRelease_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            IBusinessProject businessProject = new BusinessProject();
            IProject project = businessProject.GetProject(int.Parse(ddlProject.SelectedValue));

            IBusinessRelease businessRelease = new BusinessRelease();
            IRelease release = businessRelease.GetRelease(int.Parse(ddlProject.SelectedValue), int.Parse(ddlRelease.SelectedValue));
            lblProject.Text = project.ProjectDescription;
            lblRelease.Text = release.Description;
            lblStartDate.Text = release.StartDate.Date.ToString("MM-dd-yyyy");
            //lblEndDate.Text = release.EndDate.Date.ToString("dd/MM/yyyy");
            //DateTime.Now.Date.ToString("MM-dd-yyyy");
            lblEndDate.Text = release.EndDate.Date.ToString("MM-dd-yyyy");
            txtDbAsOfDate.Text = DateTime.Now.ToString("MM-dd-yyyy");
            IList<ITask> _tasks = _dashBoard.Tasks.OrderBy(x => x.Priority).ToList();
            gvDashBoard.DataSource = _tasks;
            gvDashBoard.DataBind();
        }

        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));
            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);
        }

        protected string getTaskStatusDescription(object statusId)
        {
            IBusinessTaskStatus businessTaskStatus = new BusinessTaskStatus();
            ITaskStatus taskStatus = businessTaskStatus.GetTaskStatus(int.Parse(statusId.ToString()));
            return taskStatus.StatusDescription;

        }

        protected void btnGetDashboard_OnClick(object sender, EventArgs e)
        {
            ViewState["DashBoard"] = null;
            IList<ITask> _tasks = _dashBoard.Tasks.OrderBy(x => x.Priority).ToList();
            gvDashBoard.DataSource = _tasks;
            gvDashBoard.DataBind();
        }

        protected void gvDashBoard_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string taskId;

            switch (e.CommandName)
            {
                case "EditActual":
                    taskId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "ShowUpdateAddTasks", "ShowUpdateAddTasks('" + taskId + "');", true);
                    //Response.Redirect("AddTasks.aspx?Mode=Edit&TaskId=" + taskId);

                    break;
            }
        }

        protected void gvDashBoard_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int _status = ((ITask)e.Row.DataItem).Status;

                

                switch (_status)
                {
                    case Constants.TaskStatus.InProgress:
                        e.Row.BackColor = Color.GreenYellow;
                        break;
                    case Constants.TaskStatus.Hold:
                        e.Row.BackColor = Color.Gray;
                        break;
                    case Constants.TaskStatus.Planned:
                        e.Row.BackColor = Color.Yellow;
                        break;

                    case Constants.TaskStatus.DevelopmentCompleted:
                        e.Row.BackColor = Color.SkyBlue;
                        break;

                    case Constants.TaskStatus.CITestCompleted:
                        e.Row.BackColor = Color.Green;
                        break;
                }

                //Label gridTaskShortName = (Label)e.Row.Cells[0].FindControl("lblTaskShortName");
                //if (gridTaskShortName != null)
                //{
                //    // Make the Label look like an anchor...
                //    gridTaskShortName.Font.Underline = true;
                //    gridTaskShortName.ForeColor = System.Drawing.Color.Blue;
                //    gridTaskShortName.Style.Add("cursor", "hand");

                //    // Create the window to open's URL here...
                //    string windowUrl = "AddTask.aspx?Mode=Edit&TaskId=" + ((ITask)e.Row.DataItem).TaskId.ToString();
                //    string clickHandler = string.Format("window.open('{0}');", windowUrl);

                //    // Attach the onclick event and handler...    
                //    gridTaskShortName.Attributes.Add("onclick", clickHandler);
                //}





            }


            if (e.Row.RowType == DataControlRowType.Footer)
            {

                e.Row.Cells[6].Text = _dashBoard.TotalInitialEstimation.ToString();
                e.Row.Cells[7].Text = _dashBoard.TotalCurrentEstimation.ToString();
                e.Row.Cells[8].Text = _dashBoard.TotalElapsed.ToString();
                e.Row.Cells[9].Text = _dashBoard.TotalRemainingForTasks.ToString();
                e.Row.Cells[10].Text = _dashBoard.TotalRemainingForRelease.ToString();
                e.Row.Cells[11].Text = _dashBoard.TotalRemainingToFinish.ToString();

            }
        }
    }
}